$().ready(function() {
	get_ads();
});
var ads_listt = new Array();
var ads_listl = new Array();
var timera = new Array();
var timerb = new Array();
var intTop = 0;
var intLeft = 0;
function get_ads(){
	xfunc = "ads/get_ads_list";
	data_sent = {};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request		
			var top_ads = new Array();
			var left_ads = new Array();
			
			top_inc = 0;
			left_inc = 0;
			htmltimer='';
			htmltimer+='<div class="hide">';
				htmltimer+='<div class="ads_timer">';
			$(result.intID).each(function(k,v){			
				htmltimer+='<a>trans_'+result.intID[k]+';'+result.intMSDelay[k]+';'+result.intOrder[k]+';'+result.intType[k]+'</a>';
				if(result.intType[k]==1){
					html='';
					html+='<div id="trans_'+result.intID[k]+'" class="box-strImgLink-ads hide">';
						html+='<div class="strImgLink-ads-top">';
							html+='<a href="'+result.strNotes[k]+'"><img class="img-rounded" src="'+base_url+"assets/uploaded/img/"+result.strImgLink[k]+'" /></a>';
						html+='</div>';
					html+='</div>';
					top_ads[top_inc]=html;
					top_inc++;
				}
				if(result.intType[k]==2){
					html='';
					html+='<div id="trans_'+result.intID[k]+'" class="box-strImgLink-ads hide">';
						html+='<div class="strImgLink-ads-left">';
							html+='<a href="'+result.strNotes[k]+'"><img class="img-rounded" src="'+base_url+"assets/uploaded/img/"+result.strImgLink[k]+'" /></a>';
						html+='</div>';
					html+='</div>';
					left_ads[left_inc]=html;
					left_inc++;
				}				
			});
				htmltimer+='</div>';
			htmltimer+='</div>';
			$('body').append(htmltimer);
			$('.box-wrapper-ads-top').html('');
			for(x=0;x<=top_inc;x++){
				$('.box-wrapper-ads-top').append(top_ads[x]);
			}
			$('.box-wrapper-ads-right').html('');
			for(x=0;x<=left_inc;x++){
				$('.box-wrapper-ads-right').append(left_ads[x]);
			}
			clean_ads();
		}
	});	
}
function clean_ads(){
	chk = $('.ads_timer a').length;
	inca = 0;
	incb = 0;
	if(chk>0){
		$('.ads_timer a').each(function(){
			var list = $.trim($(this).text());
			var list_s = list.split(';');
			if(list_s[3]==1){
				ads_listt[list_s[2]]=list_s[0]+';'+list_s[1];
			}
			if(list_s[3]==2){
				ads_listl[list_s[2]]=list_s[0]+';'+list_s[1];
			}			
		});
		for(x=0;x<=ads_listt.length;x++){
			if(ads_listt[x]!=undefined){
				timera[inca] = ads_listt[x];
				inca++;
			}
		}
		for(x=0;x<=ads_listl.length;x++){
			if(ads_listl[x]!=undefined){
				timerb[incb] = ads_listl[x];
				incb++;				
			}
		}
		ca = timera[0].split(';');
		$('#'+ca[0]).show();
		$('#'+ca[0]).fadeOut(ca[1]*1,function(){
			intTop = 0;
			show_ads_top();
		});	
		ca = timerb[0].split(';');
		$('#'+ca[0]).show();
		$('#'+ca[0]).fadeOut(ca[1]*1,function(){
			intLeft = 0;
			show_ads_left();
		});			
	}	
}
function show_ads_top(){
	intTop+=1;
	if(timera[intTop]!=undefined){
		ca = timera[intTop].split(';');
		$('#'+ca[0]).show();
		$('#'+ca[0]).fadeOut(ca[1]*1,function(){
			show_ads_top(intTop);
		});	
	} else {
		ca = timera[0].split(';');
		$('#'+ca[0]).show();
		$('#'+ca[0]).fadeOut(ca[1]*1,function(){
			intTop = 0;
			show_ads_top(0);
		});
	}
}
function show_ads_left(){
	intLeft+=1;
	if(timerb[intLeft]!=undefined){
		ca = timerb[intLeft].split(';');
		$('#'+ca[0]).show();
		$('#'+ca[0]).fadeOut(ca[1]*1,function(){
			show_ads_left(intLeft);
		});	
	} else {
		ca = timerb[0].split(';');
		$('#'+ca[0]).show();
		$('#'+ca[0]).fadeOut(ca[1]*1,function(){
			intLeft = 0;
			show_ads_left();
		});
	}
}